package com.ilink.ilinkcommon.utils.oss;

/**
 * <h3>iLink-java-backend-zhangbo2</h3>
 *
 * @Description <p>定义OSS的基本信息</p>
 * @Author ZhangBo
 * @Date 2024-09-15 18:23
 **/

import com.aliyun.oss.OSS;
import com.aliyun.oss.OSSClientBuilder;
import javax.annotation.PostConstruct;
import javax.annotation.PreDestroy;
import lombok.Getter;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

/**
 * @ClassName OssClient
 * @Description 定义OSS的基本信息
 * @Author ZhangBo
 * @Date 2024-09-15 18:23
 **/
@Slf4j
@Component
public class OssClient {

    @Value("${ilink.oss.endpoint}")
    private String endpoint;

    @Value("${ilink.oss.access-key-id}")
    private String accessKeyId;

    @Value("${ilink.oss.access-key-secret}")
    private String accessKeySecret;

    @Getter
    @Value("${ilink.oss.bucket-name}")
    private String bucketName;

    private OSS ossClient;
    private boolean isShutdown = false;

    @PostConstruct
    public void init() {
        ossClient = new OSSClientBuilder().build(endpoint, accessKeyId, accessKeySecret);
    }

    public OSS getClient() {
        return ossClient;
    }

    @PreDestroy
    public void shutdown() {
        if (ossClient != null && !isShutdown) {
            ossClient.shutdown();
            isShutdown = true;
            log.info("OSS client has been shut down.");
        }
    }

}
